import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
} from '@fast-crud/fast-crud';

import { ref } from 'vue';

import { $t } from '@vben/locales';

import { dict } from '@fast-crud/fast-crud';

import { DefApplicationApi, DefTenantApi } from '#/api';
import { DefTenantApplicationRecordApi } from '#/api/devOperation/application/defTenantApplicationRecord';
import { DictEnum } from '#/enums/commonEnum';
import {
  backendDict,
  createdTimeColumn,
  indexColumn,
} from '#/plugins/fast-crud/common';

export function createCrudOptions(
  props: CreateCrudOptionsProps,
): CreateCrudOptionsRet {
  const selectedRowKeys = ref<string[]>([]);

  return {
    crudOptions: {
      request: {
        pageRequest: DefTenantApplicationRecordApi.pageRequest,
      },
      actionbar: {
        buttons: { add: { show: false } },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: 80,
        buttons: {
          edit: { show: false },
          remove: { show: false },
          copy: { show: false },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        tenantId: {
          title: $t(
            'devOperation.application.defTenantApplicationRecord.tenantId',
          ),
          type: 'dict-select',
          dict: dict({
            value: 'id',
            label: 'name',
            async getData() {
              return await DefTenantApi.query({});
            },
          }),
          search: { show: true },
          form: { show: false },
          column: { show: false },
        },
        applicationId: {
          title: $t(
            'devOperation.application.defTenantApplicationRecord.applicationId',
          ),
          type: 'dict-select',
          dict: dict({
            value: 'id',
            label: 'name',
            async getData() {
              return await DefApplicationApi.query({});
            },
          }),
          search: { show: true },
          form: { show: false },
          column: { show: false },
        },
        applicationName: {
          title: $t(
            'devOperation.application.defTenantApplicationRecord.applicationName',
          ),
          type: 'text',
        },
        tenantName: {
          title: $t(
            'devOperation.application.defTenantApplicationRecord.tenantName',
          ),
          type: 'text',
        },
        operateByName: {
          title: $t(
            'devOperation.application.defTenantApplicationRecord.operateByName',
          ),
          type: 'text',
        },
        grantType: {
          title: $t(
            'devOperation.application.defTenantApplicationRecord.grantType',
          ),
          type: 'dict-radio',
          dict: backendDict(DictEnum.ApplicationGrantTypeEnum),
        },
        ...createdTimeColumn({}),
      },
    },
  };
}
